<template>
  <div class='container'>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="79px" class="demo-ruleForm">
          <el-form-item label="学科名称" prop="subjectName">
            <el-input v-model="ruleForm.subjectName" placeholder="请输入学科名称"></el-input>
          </el-form-item>
          <el-switch
          style="display: block"
          v-model="value1"
          active-color="#13ce66"
          inactive-color="#ff4949"
          inactive-text="是否显示">
        </el-switch>
          <div class="dialog-footer">
            <el-button @click="unhide">取 消</el-button>
            <el-button type="primary" @click="submitfn">确 定</el-button>
          </div>
        </el-form>

  </div>
</template>

<script>
import { add, update } from '@/api/hmmm/subjects'
export default {
  data () {
    return {
      ruleForm: { subjectName: '', id: '' },
      rules: {
        name: [{ required: true, message: '请输入学科名称', trigger: 'blur' }]
      },
      value1: true
    }
  },
  props: {
    dialogVisible: {
      type: Boolean
    },
    isades: {
      type: Boolean
    },
    row: {
      type: Object,
      default: () => {}
    }
  },
  watch: {
    isades: {
      handler (val) {
        if (val) {
          this.ruleForm = JSON.parse(JSON.stringify(this.row))
        }
      },
      immediate: true
    }
  },
  methods: {
    // 点击取消按钮
    unhide () {
      this.$emit('update:dialogVisible', false)
    },
    // 点击确认按钮
    submitfn () {
      // console.log(this.ruleForm.name, 56)
      // console.log(this.value1, 566)
      this.$refs.ruleForm.validate((valid) => {
        if (!valid) return
        this.isades ? this.upda() : this.submi()
        this.success()
      })
    },
    // 点击修改是触发的
    async upda () {
      await update(this.ruleForm, { subjectName: this.ruleForm.subjectName })
    },
    // 点击新增时触发的
    async submi () {
      await add(this.ruleForm)
    },
    success () {
      // 刷新页面和关闭对话框
      this.$emit('success')
      this.ruleForm = {}
    }
  },
  destroyed () {
    console.log(111)
  }
}
</script>

<style scoped lang='scss'>
.dialog-footer{
  margin-top: 50px;
}
</style>
